# coding=utf-8
"""
copyright (c) 2009, paketka@gmail.com et. al
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

	* Redistributions of source code must retain the above copyright notice,
	  this list of conditions and the following disclaimer.

	* Redistributions in binary form must reproduce the above copyright notice,
	  this list of conditions and the following disclaimer in the documentation
	  and/or other materials provided with the distribution.

	* Neither the name of the <ORGANIZATION> nor the names of its contributors
	  may be used to endorse or promote products derived from this software
 	  without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""
import datetime
from GChartWrapper import *

def create_pie(data):
	"""
	uses data to create a chart. data is a dictionary: values in dictionary are
	values, while keys are value names.

	Example:
		{	'women' : 20,
			'man' : 80,
		}
	will create 20/80 chart with description to each value

	"""

	retVal = Pie3D(data.values())

	args = []
	total = reduce(lambda x, y: x + y, data.values()) * 1.0
	args = map(lambda x: '%s %.2f%%' % (x, ((data[x] / total) * 100)),
					data.keys()
			)
	retVal.label(*args)

	###
	# TODO: colors must be gathered from group.
	colors = []
	colorStart = 0x55
	color = 0x55
	formatStrs = ['0000%x', '00%x00', '%x0000']
	for i in range(len(data)):
		if i % 3 == 0:
			colorStart += 0x11
			color = colorStart

		colors.append(formatStrs[i % 3] % color)

	retVal.color(*colors) 

	return retVal
	

def get_year_rng_axes(terms):
	"""
	gets string parameters for x graph axis.  it's mainly used for vertical bar
	chart, which describes comparison between office terms.
	
	these year ranges appears as two rows of values.
		start years (startY)
		end years (endY)
	below corresponding bar
	"""
	startY = ''
	endY = ''
	for t in terms:
		startY += t.yearStart + '|'
		if t.yearEnd == '':
			endY += str(datetime.datetime.today().year)
		else:
			endY += t.yearEnd + '|'
	
	return startY, endY


def get_axis(r, spaces = 0, offset = 0):
	"""
	creates an axis for a graph:
		r - values to be placed to axis
		spaces - number of spaces (empty rows/cols) between values
		offset - offset to move from beginning (nmber of empty rows/cols)
	"""
	retVal = u''
	for i in range(offset):
		retVal += u' |'

	for i in r:
		retVal += unicode(i) + u'|'

		for j in range(spaces):
			retVal += u' |'

	return retVal

	
def make_title(title, desiredLineLen):
	"""
	title is string, blockLen is approx length in chars we want to fit in
	"""
	lineLen = 0
	retVal = u''
	for word in title.split(): 
		lineLen += len(word)
		retVal += word + u' '
		
		if lineLen > desiredLineLen:
			retVal += '\n'
			lineLen = 0

	return retVal



def get_percents(part, total):
	"""
	Returns amount of part in percents. Percents are rounded to two dec. fields.
	Returns float.
	"""
	if total == 0:
		total = 1

	return (((1.0 * part) / total) * 100.0)
